package dao.impl;

import bean.pojo.ParkingRecord;
import bean.pojo.User;
import com.sun.org.apache.bcel.internal.generic.ISHR;
import commons.dbHelper.DBHelper;
import dao.ParkingRecordDao;

import java.util.ArrayList;
import java.util.List;

/**
 * bean.pojo.vehicle
 * User: 坚持
 * Date: 2024/12/18 11:34
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */

public class ParkingRecordDaoImpl implements ParkingRecordDao {


    @Override
    public List<ParkingRecord> findBy(Integer page, Integer limit, Integer vehicleId, String startParkingTime, String endParkingTime) {
        StringBuffer sql = new StringBuffer("select parking_record_id,vehicle_id,parking_space_id,start_parking_time,end_parking_time from parking_record where 1=1");
        List<Object>  parameters=new ArrayList<>();
        if (vehicleId != null) {
            sql.append(" and vehicle_id=? ");
            parameters.add(vehicleId);
        }
        if (startParkingTime != null && endParkingTime!=null) {
            sql.append(" and start_parking_time >= ?  and end_parking_time <= ? ");
            parameters.add(startParkingTime);
            parameters.add(endParkingTime);
        } else if (startParkingTime != null) {
            sql.append(" and start_parking_time >= ?  ");
            parameters.add(startParkingTime);
        }else if(endParkingTime!=null){
            sql.append(" and end_parking_time <= ?  ");
            parameters.add(endParkingTime);
        }
        sql.append(" order by parking_record_id asc ");
        sql.append(" limit " + (page - 1) * limit + "," + limit);
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        return dbHelper.getBeanList(ParkingRecord.class,sql.toString(),parameters.toArray());
    }

    @Override
    public Long findByTotal(Integer vehicleId, String startParkingTime, String endParkingTime) {
        StringBuffer sql = new StringBuffer("select parking_record_id,vehicle_id,parking_space_id,start_parking_time,end_parking_time from parking_record where 1=1");
        List<Object>  parameters=new ArrayList<>();
        if (vehicleId != null) {
            sql.append(" and vehicle_id=? ");
            parameters.add(vehicleId);
        }
        if (startParkingTime != null && endParkingTime!=null) {
            sql.append(" and start_parking_time >= ?  and end_parking_time <= ? ");
            parameters.add(startParkingTime);
            parameters.add(endParkingTime);
        } else if (startParkingTime != null) {
            sql.append(" and start_parking_time >= ?  ");
            parameters.add(startParkingTime);
        }else if(endParkingTime != null){
            sql.append(" and end_parking_time <= ?  ");
            parameters.add(endParkingTime);
        }

        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        List<ParkingRecord> query =  dbHelper.getBeanList(ParkingRecord.class,sql.toString(),parameters.toArray());
        if (query!=null){
            return (long)query.size();
        }
        return 0L;
    }

    @Override
    public Integer update(ParkingRecord parkingRecord) {
        String sql ="update parking_record set vehicle_id=?,parking_space_id=?," +
                "start_parking_time=?,end_parking_time=? where parking_record_id=?";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,parkingRecord.getVehicleId(),parkingRecord.getParkingSpaceId(),
                parkingRecord.getStartParkingTime(),parkingRecord.getEndParkingTime(),parkingRecord.getParkingRecordId());
    }

    @Override
    public Integer deleteById(Integer parkingRecordId) {
        String sql = " delete from parking_record where parking_record_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,parkingRecordId);
    }

    @Override
    public Integer add(ParkingRecord parkingRecord) {
        String sql ="insert parking_record  (vehicle_id,parking_space_id,start_parking_time," +
                "end_parking_time) values (?,?,?,?)";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,parkingRecord.getVehicleId(),parkingRecord.getParkingRecordId(),
                parkingRecord.getStartParkingTime(),parkingRecord.getEndParkingTime());
    }
}
